package com.farm.service.impl;

import com.farm.constant.MessageConstant;
import com.farm.constant.StatusConstant;
import com.farm.dto.GoodsDTO;
import com.farm.dto.GoodsPageQueryDTO;
import com.farm.entity.Category;
import com.farm.entity.Goods;
import com.farm.exception.DeletionNotAllowedException;
import com.farm.mapper.CategoryMapper;
import com.farm.mapper.GiftGoodsMapper;
import com.farm.mapper.GoodsMapper;
import com.farm.result.PageResult;
import com.farm.service.GoodsService;
import com.farm.vo.GoodsVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName GoodsServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-02-24
 **/

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private GiftGoodsMapper giftGoodsMapper;


    /**
     * 新增商品
     *
     * @param goodsDTO
     */
    @Override
    public void save(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO, goods);

        goodsMapper.insert(goods);
    }

    /**
     * 商品分页查询
     *
     * @param goodsPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(GoodsPageQueryDTO goodsPageQueryDTO) {
        PageHelper.startPage(goodsPageQueryDTO.getPage(), goodsPageQueryDTO.getPageSize());
        Page<Goods> page = goodsMapper.page(goodsPageQueryDTO);

        List<GoodsVO> goodsVoList = new ArrayList<>();

        page.forEach(goods -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(goods, goodsVO);
            Long categoryId = goods.getCategoryId();
            Category category = categoryMapper.getById(categoryId);
            goodsVO.setCategoryName(category.getName());
            goodsVoList.add(goodsVO);
        });

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(goodsVoList);

        return pageResult;
    }

    /**
     * 商品起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Goods goods = Goods.builder()
                .id(id)
                .status(status)
                .build();

        goodsMapper.update(goods);
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @Override
    public GoodsVO getById(Long id) {
        Goods goods = goodsMapper.getById(id);
        Category category = categoryMapper.getById(goods.getCategoryId());
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods, goodsVO);
        goodsVO.setCategoryName(category.getName());

        return goodsVO;
    }

    /**
     * 修改商品
     *
     * @param goodsDTO
     */
    @Override
    public void update(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO, goods);

        goodsMapper.update(goods);
    }


    /**
     * 批量删除商品
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        List<Goods> goodsList = goodsMapper.getByIds(ids);

        for (Goods goods : goodsList) {
            if (goods.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.GOODS_ON_SALE);
            }
        }

        List<Long> giftIds = giftGoodsMapper.getGiftIdsByGoodsIds(ids);

        if (giftIds != null && giftIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.GOODS_BE_RELATED_BY_GIFT);
        }

        goodsMapper.deleteByIds(ids);

    }

    /**
     * 根据分类id查询商品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Goods> list(Long categoryId) {
        Goods goods = Goods.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Goods> goodsList = goodsMapper.list(goods);
        return goodsList;
    }

    /**
     * 根据分类id查询商品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<GoodsVO> getByCategoryId(Long categoryId) {
        List<Goods> goods = goodsMapper.getByCategoryId(categoryId);

        List<GoodsVO> goodsVoList = new ArrayList<>();
        goods.forEach(g -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(g, goodsVO);
            Category category = categoryMapper.getById(g.getCategoryId());
            goodsVO.setCategoryName(category.getName());
            goodsVoList.add(goodsVO);
        });

        return goodsVoList;
    }
}
